/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Normal web interface:
 *  * http://www.zdf.de/ARDtext/deep/master.php?zaehler=100&subpage=1
 */

#include "Teletext.h"
#include "ARDTeletext.h"

#include <QtDebug>

const QString ARDTeletext::TEXTNAME("ARDTeletext");

/**
 * \short Constructor of the base Teletext object. 
 * 
 * Constructor of the base Teletext object. 
 */

ARDTeletext::ARDTeletext(PageInfo *p) : Teletext() {
  channelname = ARDTeletext::TEXTNAME;

  pageinfo = p;
  qnam = new QNetworkAccessManager(this);
  QObject::connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
}

/**
 * \short Destructor of the base Teletext object. 
 * 
 * Destructor of the base Teletext object. 
 */

ARDTeletext::~ARDTeletext() {

}

/**
 * Return a pixmap to the current page
 */

void ARDTeletext::getTextPage(int page, int subpage) {
  _subpage = subpage;
  QString url;
  if(subpage != 1) {
    url.sprintf("http://www.ard-text.de/index.php?page=%d&sub=%d", page, subpage);
  } else {
    url.sprintf("http://www.ard-text.de/index.php?page=%d", page);
  }

  QNetworkRequest request;
  request.setUrl(QUrl(url));
  request.setRawHeader("User-Agent", USER_AGENT);
  reply = qnam->get(request);
}

/**
 * \short Slot function for a finished download
 * 
 * \param p Pointer to the QNetworkReply
 * 
 * Set the pixpage to the downloaded image. 
 * This function needs to be overridden if the download is not an image. 
 */

void ARDTeletext::downloadFinished(QNetworkReply* reply) {
  qDebug() << "ARDTeletext::downloadFinished() - url: " << reply->request().url();
  //qDebug() << "ARDTeletext::downloadFinished() - content: \n" << reply->readAll();
  QDomDocument *doc = new QDomDocument();
  doc->setContent(reply->readAll());
  // Return the root element (html)
  QDomElement root = doc->documentElement();
  QDomNode body = root.firstChild();


  /*

  QDomNode npage = root.firstChild();
  QDomNode subnpage = npage.firstChild();
  // Check if we have a subnpage with id _subpage
  for(int n = 0; n < MAX_SUB_PAGE_RANGE; n++) {
    if(subnpage.toElement().attribute("number").toInt() != _subpage) {
      subnpage = subnpage.nextSibling();
    } else {
      break;
    }
  }
  if(subnpage.isNull()) {
    // There is no subpage with the requested id
    return;
  }

  // Get the node with type="all"
  QDomNode content = subnpage.firstChild();
  for(int n = 0; n < 2; n++) {
    //if(content.toElement().attribute("type") != QString("all")) { // Contains color information
    if(content.toElement().attribute("type") != QString("text")) { // Simple plain text alternative
      content = content.nextSibling();
    } else {
      break;
    }
  }
  if(content.isNull()) {
    // There is no node with type="all"
    return;
  }
  */
  /*
  // The corrent part of the XML is found - it can now be parsed, line by line. 
  // Create the painter - target is 24 lines, so 400/24=16 pixels per line
  QImage image(400, 300, QImage::Format_RGB32);
  QPainter painter(&image);
  int height = 16;
  int top = 5;

  // walk on all lines
  QDomNode line = content.firstChild();

  // Painter settings
 painter.setPen(Qt::yellow);
 painter.setBrush(QBrush(Qt::black, Qt::SolidPattern));
 painter.setFont(QFont("Courier New", 9));
 painter.setBackgroundMode(Qt::TransparentMode);
 painter.drawRect(-1, -1, 401, 301);

  while(!line.nextSibling().isNull()) {
    line = line.nextSibling();
    QRectF rectangle(QPoint(25, top), QPoint(400, top + 14));
    painter.drawText(rectangle, Qt::AlignLeft, line.toElement().text());
    top+=13;
  }

  pixpage = new QPixmap(QPixmap::fromImage(image));
  if(pixpage != NULL) {
    pageinfo->setTeletextPage(pixpage);
  }
  */
  // Free the QDomDocument
  // And the reply
  delete doc;
  reply->deleteLater();
}
